package com.jsh.erp.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.UUID;

public class UUIDUtil {

    // 将字符串转换为UUID的方法（与之前提供的相同）
    public static UUID generateUuidFromString(String originalString) throws Exception {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] hashBytes = digest.digest(originalString.getBytes(StandardCharsets.UTF_8));
        long mostSigBits = 0;
        long leastSigBits = 0;
        for (int i = 0; i < 8; i++) {
            mostSigBits = (mostSigBits << 8) | (hashBytes[i] & 0xFF);
        }
        for (int i = 8; i < 16; i++) {
            leastSigBits = (leastSigBits << 8) | (hashBytes[i] & 0xFF);
        }
        mostSigBits &= 0xffffffffffff0fffL;
        mostSigBits |= 0x0000000000004000L;
        leastSigBits &= 0x3fffffffffffffffL;
        leastSigBits |= 0x8000000000000000L;
        return new UUID(mostSigBits, leastSigBits);
    }
}
